#!/usr/bin/env python
# -*— coding: utf-8 -*-

from multiprocessing import Pool
import os,time,random

def worker(msg):
  tStart = time.time()
  print("%s开始执行，进程号为%d"%(msg, os.getpid()))
  time.sleep(random.random()*2)
  tStop = time.time()
  print(msg, "执行完毕，耗时%0.2f"%(tStop-tStart))

po = Pool(3) #最大进程数为3

for i in range(0, 10):
  #po.apply_async(worker, (i,)) #添加任务 非阻塞
  po.apply(worker,(i,)) # 阻塞


print('---start---')
po.close() #关闭进程池，po不再接受新的请求
po.join() #等待po中所有子进程执行完毕，必须放在close语句之后
print('---end---')
